#region License
/* Copyright (c) 2011 Maksymilian Mulawa
	
Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:

   1. Redistributions of source code must retain the above copyright notice, 
 * this list of conditions and the following disclaimer.
   2. Redistributions in binary form must reproduce the above copyright notice, 
 * this list of conditions and the following disclaimer in the documentation 
 * and/or other materials provided with the distribution.
   3. The name of the author may not be used to endorse or promote products 
 * derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 
 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#endregion

using System;
using System.Data.SqlClient;
using ReplicationExplorer.DomainModel.Distributor.Repositories;
using ReplicationExplorer.DomainModel.Infrastructure;
using ReplicationExplorer.DomainModel.Publisher.Repositories;
using ReplicationExplorer.DomainModel.Subscriber.Repositories;
using ReplicationExplorer.Repositories;

namespace ReplicationExplorer.DomainModel.Distributor.Entities
{
    public class DistributorSession : IDistributorSession
    {
        private readonly UserCredentials _user;
        private readonly SqlServerInfo _server;
        private string _distributorConnectionString;

        public DistributorSession(UserCredentials user, SqlServerInfo server)
        {
            _user = user;
            _server = server;
            DistributorRepository = new DistributorRepository(this);
            PublisherRepository = new PublisherRepository(this);
            SubscriptionRepository = new SubscriptionRepository(this);
        }

        public string DistributorConnectionString
        {
            get
            {
                if(string.IsNullOrEmpty(_distributorConnectionString))
                {
                    _distributorConnectionString = BuildDistributorConnectionString("master");
                }
                return _distributorConnectionString;
            }
        }

        public int SqlQueryTimeoutInSeconds
        {
            get { return Properties.Settings.Default.SqlQueryTimeout; }
        }

        public int SqlServerConnectionTimeoutInSeconds
        {
            get { return Properties.Settings.Default.ServerTimeout; }
        }

        public SqlServerInfo Server
        {
            get { return _server; }
        }

        public UserCredentials User
        {
            get { return _user; }
        }

        public IDistributorRepository DistributorRepository { get; private set; }

        public IPublisherRepository PublisherRepository { get; private set; }

        public ISubscriptionRepository SubscriptionRepository { get; private set; }

        public DistributorServer GetDistributor()
        {
            DistributorServer distributorServer = DistributorRepository.GetDistributor();
            distributorServer.DistributorSession = this;
            return distributorServer;
        }

        public string GetDistributionDatabaseConnectionString(string distributionDatabase)
        {
            return BuildDistributorConnectionString(distributionDatabase);
        }

        private string BuildDistributorConnectionString(string initialCatalog)
        {
            SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
            builder.ConnectTimeout = SqlServerConnectionTimeoutInSeconds;
            builder.DataSource = Server.ServerName;
            builder.InitialCatalog = initialCatalog;
            builder.IntegratedSecurity = User.IsWindowsAuthenticated;
            builder.MultipleActiveResultSets = true;
            if(!User.IsWindowsAuthenticated)
            {
                builder.UserID = User.UserId;
                builder.Password = User.Password;
            }
            return builder.ConnectionString;
        }
    }
}